<?php

class Release extends Controller {
	
	/**
	 * Constructor
	 */
	function Release(){
		parent::Controller();
		$this->load->model('Release_model');
	}
	
	/**
	 * Add a new release to the database
	 */
	function add_release($branchid){
		if (is_numeric($branchid) && $this->auth->has_rights_to("add", "rls", $branchid))
		{
			$config = array ('upload_path' => $this->config->item('uploads'),
							 'allowed_types' => 'jpg|png|txt|zip|jar',
							 'max_size'	=> '2000');
			$this->load->library('upload', $config);
			
			//if there was a problem with upload
			if (!$this->upload->do_upload()){
				$param['errors']['file_err']= $this->upload->display_errors('<p>', '</p>');	
				$problem_upload = true;
			}
			else {
				$problem_upload = false;
				$param['errors']['file_err']='';
			}
			
			$infos = array(
						'version' => $this->input->post('version'),
    					'release_note' => $this->input->post('release_note'),
    					'type' => $this->input->post('type'));
			
			$result = array();
			if(!$problem_upload){
				//Adding the release
				$result = $this->Release_model->new_release($infos, $branchid);
				if (!is_array($result)) {
					$releaseInfos = $this->save_release_file($branchid, $config['upload_path']);
					//there still may have been errors moving the file, though it would then be due to a structural error
					$this->load->view('release/addOk', $releaseInfos);
				}
				else {
					$param['errors'] = array_merge($param['errors'], $result);
					$problem_upload = true;
				}
			}
			if ($problem_upload) {
				$param['branchid'] = $branchid;
				$param['values'] = array();
				foreach($infos as $key => $value){
					if(isset($result[$key])){
						$param['values'][$key.'_val'] = '';
					}else{
						$param['values'][$key.'_val'] = $value;
					}
				}
				
				if(!isset($param['errors']))
					$param['errors'] = array();
				foreach($infos as $key => $value){
					$param['errors'][$key.'_err'] = '';
				}
				foreach($result as $key => $value){
					$param['errors'][$key.'_err'] = $value;
				}
				$this->load->view('release/add', $param);
			}
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	private function save_release_file($branchid, $filepath) {
		$this->load->library('zip');
		$this->load->model('Branch_model');
		$this->load->model('Package_model');
		$releaseInfos = $this->Release_model->data;
		
		$this->Branch_model->load_branch_from_id($branchid);
		$branchInfos = $this->Branch_model->data;
		
		$packageInfos = $this->Package_model->load_package_from_id($branchInfos['package']);
		$packageInfos = $this->Package_model->data;
		
		$data = array('upload_data' => $this->upload->data());
		
		$filename = $packageInfos['name'] . $releaseInfos['version'] . '.cpkg';
		
		// add the release in zip archive
		$this->zip->read_file($filepath . $data['upload_data']['file_name']); 

		// Write the zip file to a folder on your server. Name it "my_backup.zip"
		$this->zip->archive($filepath . $filename);
		$md5 = md5_file($filepath.$filename);
		
		// making the download url
		$url = $this->config->item('download') . 
				$packageInfos['pkg_id'] . '/' .
				$branchInfos['branch_id'] . '/' .
				$releaseInfos['rls_id'] . '/' . $filename;
		
		// moving the uploaded file
		mkdir($this->config->item('download') . 
				$packageInfos['pkg_id'] . '/' .
				$branchInfos['branch_id'] . '/' .
				$releaseInfos['rls_id'] . '/');
		rename($filepath . $filename, $url);
		unlink($filepath . $data['upload_data']['file_name']);

		// update release url
		$this->Release_model->save_release($releaseInfos['rls_id'], array('url' => $url, 'rlshash' => $md5));
		return $releaseInfos;
	} 
	
	/**
	 * Display the add release form
	 */
	function iadd($branchid){
		if (is_numeric($branchid) && $this->auth->has_rights_to("add","rls", $branchid))
		{
			$param['values'] = array('version_val' => '', 'release_note_val' => '', 'file_val' => '', 'type_val' => '');
			$param['errors'] = array('version_err' => '', 'release_note_err' => '', 'file_err' =>'', 'type_err' => '');
			$param['branchid']=$branchid;
			$this->load->view('release/add',$param);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the add release form into an iframe
	 */
	function add($branchid){
		if (is_numeric($branchid) && $this->auth->has_rights_to("add","rls", $branchid))
		{
			echo '<iframe src="'.site_url("release/iadd").'/'.$branchid.'" name="iform" style="border-style:none;" HEIGHT="500" WIDTH="700"/>';
		}	
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the information of a release
	 */
	function load($id){
		if (is_numeric($id) && $this->auth->has_rights_to("lookat", "rls", $id))
		{
			$this->Release_model->load_release_from_id($id);
			$data = $this->Release_model->data;
			if (!is_array($data))
     			$this->load->view('system_error');
     		else
				$this->load->view('release/show',$data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the information of a release
	 */
	function loadRelease($id){
		if (is_numeric($id) && $this->auth->has_rights_to("lookat","rls", $id))
		{
			$data['releaselist'] = $this->Release_model->get_releases($id, null, null, array('status' => 'active'));
			if (!is_array($data['releaselist']))
     			$this->load->view('system_error');
     		else
				$this->load->view('release/list',$data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	
	/**
	 * Display the release list
	 */
	function releaselist($branchid){
		if (is_numeric($branchid) && $this->auth->has_rights_to("add","rls"))
		{
			$data['releaselist'] = $this->Release_model->get_releases($branchid, null, null, array('status' => 'active'));
			if (!is_array($data['releaselist']))
     			$this->load->view('system_error');
     		else
				$this->load->view('release/list',$data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the edit release view in order to edit the release $id
	 */
	function edit($id){
		if (is_numeric($id) && $this->auth->has_rights_to("edit","rls", $id))
		{
			$this->Release_model->load_release_from_id($id);
			$data = $this->Release_model->data;
			if (!is_array($data))
     			$this->load->view('system_error');
     		else
				$this->load->view('release/edit',$data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Save the new information of the release into the database
	 */
	function edit_release($rls_id){
		if (is_numeric($rls_id) && $this->auth->has_rights_to("edit","rls", $rls_id))
		{
			$infos = array ('version' => $this->input->post('version'),
							'release_note' => $this->input->post('release_note'));
			if (!$this->Release_model->save_release($rls_id,$infos))
     			$this->load->view('system_error');
     		else
     			$this->load->view('editOk');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}

	
	/**
	 * Download the release
	 * @param rls_id release id
	 */
	function download($rls_id){
		if (is_numeric($rls_id) && $this->auth->has_rights_to("list","rls", $rls_id))
		{
			$this->Release_model->load_release_from_id($rls_id);
			$releaseInfos = $this->Release_model->data;
			if (!is_array($releaseInfos))
     			$this->load->view('system_error');
     		else
				header('Location: '.$this->config->item('base_url').$releaseInfos['url']);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Delete the release $rls_id from the database
	 */
	function delete($rls_id) {
		if (is_numeric($rls_id) && $this->auth->has_rights_to("delete","rls", $rls_id))
		{			
			if (!$this->Release_model->save_release($rls_id, array('status' => 'deleted')))
     			$this->load->view('system_error');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Displays a dropdown containing the branches attached to a given package
	 */
	function list_releases_dd($bchid) {
		if (is_numeric($bchid) && $this->auth->has_rights_to("list","rls"))
		{
			$data['releases'] = $this->Release_model->get_releases($bchid, null, null, array('status' => 'active'));
			if (!is_array($data['releases']))
     			$this->load->view('system_error');
     		else
				$this->load->view('branch/relations/list_rls_dd', $data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
}
?>
